<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class order extends core {
	private $order_mod;

	public function __construct() {
		parent::__construct();
		$this->order_mod = m("order");
	}

	//订单列表
	public function index() {
		$where = " AND o.shop_id='{$this->shopadmin_id}'";
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$order_id = (isset($_GET['order_id'])) ? trim($_GET['order_id']) : '';

		if ($keyword) {
			$where .= "AND (order_sn LIKE '%{$keyword}%' OR o.name LIKE '%{$keyword}%' OR o.mobile LIKE '%{$keyword}%' OR o.address LIKE '%{$keyword}%')";
		}
		if (strlen($status)) {
			$where .= "AND o.status='{$status}'";
		}
		if (strlen($order_id)) {
			$where .= "AND o.id='{$order_id}'";
		}
		//分页
		$this->setpages(array('order_id'=>$order_id, 'keyword'=>$keyword, 'status'=>$status));
		$sql = "SELECT o.*, b.name as beautician_name, s.name as shop_name 
			FROM jf_order o LEFT JOIN im_beautician b ON o.beautician_id=b.id 
			LEFT JOIN jf_beautyshop s ON o.shop_id=s.id 
			WHERE 1 {$where} ORDER BY o.id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->order_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('order_id',$order_id);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);

		$this->display();
	}

	//详情
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$order_id = (isset($_GET['order_id']) && intval($_GET['order_id'])) ? intval($_GET['order_id']) : 0;
			$sql = "SELECT o.*, b.name as beautician_name, s.name as shop_name 
				FROM jf_order o LEFT JOIN im_beautician b ON o.beautician_id=b.id 
				LEFT JOIN jf_beautyshop s ON o.shop_id=s.id 
				WHERE o.id='{$order_id}' AND o.shop_id='{$this->shopadmin_id}'";
			// echo $sql;
			$order = $this->db->get_row($sql);
			if (!$order) $this->error('记录不存在');
			$order->status_name = $this->order_mod->status_name($order->status);
			//获取订单的项目
			$sql = "SELECT op.* FROM jf_order_project op WHERE order_id='{$order_id}'";
			$projects = $this->db->get_results($sql);
			$projects = add_domain_deep($projects, array("project_pic"));
			$order->projects = $projects;
			//订单的评价
			$stars = 0;
			$sql = "SELECT c.*, m.avatar FROM jf_order_comment c LEFT JOIN im_member m ON c.member_id=m.id 
				WHERE c.order_id='{$order_id}'";
			$comment = $this->db->get_row($sql);
			if ($comment) {
				$comment = add_domain_deep($comment, array("avatar"));
				$comment->add_time = date("Y-m-d H:i:s", $comment->add_time);
			}
			$order->comment = $comment;
			//显示
			$this->smarty->assign('row',$order);
			$this->display();
		}
	}

	//保存
	public function save() {
		$commission_mod = m("commission");
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$full_address = (isset($_POST['full_address']) && trim($_POST['full_address'])) ? trim($_POST['full_address']) : '';
		$service_time = (isset($_POST['service_time']) && trim($_POST['service_time'])) ? trim($_POST['service_time']) : '';
		$status = isset($_POST['status']) ? intval($_POST['status']) : -1;
		$origin_status = isset($_POST['origin_status']) ? intval($_POST['origin_status']) : -1;
		$sql = "UPDATE jf_order SET full_address='{$full_address}', service_time='{$service_time}', status='{$status}' 
				WHERE id='{$id}' AND shop_id='{$this->shopadmin_id}'";
		$this->db->query($sql);
		//状态发生了变化，需要处理佣金
		//增加佣金
		if ($status >= 4 && $origin_status < 4) {
			$commission_mod->order_commission($id);
		//减少佣金
		} elseif ($origin_status >=4 && $status < 4 ) {
			$commission_mod->order_commission($id,-1);
		}
		//exit;
		header("Location:?app=order&act=edit&order_id={$id}");
	}

	//删除
	public function delete() {
		$order_id = (isset($_GET['order_id']) && trim($_GET['order_id'])) ? intval($_GET['order_id']) : 0;
		$sql = "DELETE FROM jf_order WHERE id='{$order_id}' AND shop_id='{$this->shopadmin_id}'";
		$this->db->query($sql);
		header("Location:?app=order&act=index");
	}	

	//导出
	public function export() {
		$fields = array(
			'id'=>'ID',
			'order_sn'=>'订单号',
			'name'=>'顾客姓名',
			'mobile'=>'电话',
			'beautician_name'=>'美容师',
			'shop_name'=>'门店',
			'province'=>'省',
			'city'=>'市',
			'district'=>'区',
			'address'=>'地址',
			'room_number'=>'门牌号',
			'service_time'=>'服务时间',
			'add_time'=>'下单时间',
			'paid_time'=>'支付时间',
			'cancel_time'=>'取消时间',
			'confirm_time'=>'确认时间',
			'begin_time'=>'开始时间',
			'end_time'=>'结束时间',
			'total_price'=>'订单总价',
			'pay_method'=>'支付方式',
			'status_name'=>'订单状态',
			'projects'=>'项目',
			//'meals'=>'套餐',
		);
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT o.*, b.name as beautician_name, s.name as shop_name 
			FROM jf_order o LEFT JOIN im_beautician b ON o.beautician_id=b.id 
			LEFT JOIN jf_beautyshop s ON o.shop_id=s.id 
			WHERE 1 {$where} ORDER BY o.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]->status_name = $this->order_mod->status_name($row->status);
				$rs[$key]->province = $this->db->get_var("SELECT name FROM jf_province WHERE province_id='".$row->province."'");
				$rs[$key]->city = $this->db->get_var("SELECT name FROM jf_city WHERE city_id='".$row->city."'");
				$rs[$key]->district = $this->db->get_var("SELECT name FROM jf_district WHERE district_id='".$row->district."'");
				$sql = "SELECT * FROM jf_order_project WHERE order_id='".$row->id."'";
				$project_names = "";
				$projects = $this->db->get_results($sql);
				if ($projects) {
					foreach ($projects as $j => $p) {
						$project_names .= $p->project_name;
						if ($p->meal_id>0) {
							$project_names .= "(使用套餐)";
						}
						$project_names .= "\n";
					}
				}
				$rs[$key]->projects = $project_names;
				if ($row->add_time) $rs[$key]->add_time = date("Y-m-d H:i:s", $row->add_time);
				if ($row->paid_time) $rs[$key]->paid_time = date("Y-m-d H:i:s", $row->paid_time);
				if ($row->cancel_time) $rs[$key]->cancel_time = date("Y-m-d H:i:s", $row->cancel_time);
				if ($row->confirm_time) $rs[$key]->confirm_time = date("Y-m-d H:i:s", $row->confirm_time);
				if ($row->begin_time) $rs[$key]->begin_time = date("Y-m-d H:i:s", $row->begin_time);
				if ($row->end_time) $rs[$key]->end_time = date("Y-m-d H:i:s", $row->end_time);
			}
			export_excel($rs, $fields, "order");
		}
	}
}
